package com.cjjd.c.feature.subscribe.select_hotel;

import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.cjjd.c.R;
import com.cjjd.c.base.MyBaseAdapter;
import com.cjjd.c.bean.subscribe.HotelStarBean;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ----------------------------------
 * Created by xyd on 2017/12/23.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class HotelStarAdapter extends MyBaseAdapter {

    public HotelStarAdapter(FragmentActivity mContext) {
        super(mContext);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_star_select, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final HotelStarBean itemBean = (HotelStarBean) getItem(position);

        viewHolder.cbStar.setText(itemBean.starName);
        viewHolder.cbStar.setChecked(itemBean.isSelected);
        viewHolder.cbStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0;i<getCount();i++){
                    if (position != i){
                        HotelStarBean bean  = (HotelStarBean) data.get(i);
                        bean.isSelected = false;
                    }
                }
                itemBean.isSelected = !itemBean.isSelected;

                notifyDataSetChanged();
            }
        });




        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.cb_star)
        CheckBox cbStar;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
